package com.wcy.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.domain.Config;
import com.wcy.model.R;
import com.wcy.service.ConfigService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>Title:ConfigController.java</p >
 * <p>Description: </p >
 * <p>Copyright: 公共服务与应急管理战略业务本部 Copyright(c)2020</p >
 * <p>Date:2021/9/27 22:38</p >
 *
 * @author wcy (wangchaoyanga@mail.taiji.com.cn)
 * @version 1.0
 */
@RestController
@RequestMapping("/configs")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    @GetMapping
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页大小"),
            @ApiImplicitParam(name = "type",value = "规则类型"),
            @ApiImplicitParam(name = "code",value = "规则代码"),
            @ApiImplicitParam(name = "name",value = "规则名称")
    })
    @PreAuthorize("hasAuthority('config_query')")
    public R<Page<Config>> findByPage(@ApiIgnore Page<Config> page,String type,String code,String name){
        Page<Config> configPage=configService.findByPage(page,type,code,name);
        return R.ok(configPage);
    }

    @PostMapping
    @ApiOperation(value = "新增一个参数")
    @PreAuthorize("hasAuthority('config_create')")
    public R add(@RequestBody Config config){
        boolean save = configService.save(config);
        if(save){
            return R.ok();
        }
        return R.fail("新增失败");
    }

    @PatchMapping
    @ApiOperation(value = "修改一个参数")
    @PreAuthorize("hasAuthority('config_update')")
    public R update(@RequestBody Config config){
        boolean update = configService.updateById(config);
        if(update){
            return R.ok();
        }
        return R.fail("修改失败");
    }
}
